//
//  ViewController.m
//  22-AFNetworking
//
//  Created by 曹曹 on 16/8/12.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"

#define kAllCarDataURL @"http://app.api.autohome.com.cn/autov4.6/cars/brands-a2-pm2-v4.6.1-ts635580073583652962.html"
#define kPostURLString @"http://open3.bantangapp.com/base/app/init"
#define kPostURLParameter @"app_id=com.jzyd.BanTang&app_installtime=1467276561&app_versions=5.8.5&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=1aa2741a8e58dbf700d96e588b551a5bf7f8a1e222eb9948c836ba717ce0d48e&os_versions=10.0&screensize=750&track_device_info=iPhone8%2C1&track_deviceid=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&v=17"
#define kQQ_MV_URLString @"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag"
#define kQQ_MV_URLParameter @"cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self networkGET];
    [self networkPOST];
}
#pragma mark - AFNetworking POST
- (void)networkPOST {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 设置请求参数的字符串格式, 自定义
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];
    // 正常POST
    [manager POST:kQQ_MV_URLString parameters:kQQ_MV_URLParameter progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.URL);
        NSLog(@"%@", task.currentRequest.HTTPBody);
        NSLog(@"请求成功:%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败:%@", error);
    }];
}
#pragma mark - AFNetworking GET
- (void)networkGET {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
#pragma mark - 1.requestSerializer
    // 格式化body/参数
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    // 设置请求头, 设置requestSerializer中的mutableHTTPRequestHeaders字典
    [manager.requestSerializer setValue:@"" forHTTPHeaderField:@""];
    // 超时时间, 默认60s
    manager.requestSerializer.timeoutInterval = 90;
    
#pragma mark - 2.responseSerializer
    // 返回数据, 进行格式化
    // responseSerializer 默认类型 AFJSONResponseSerializer
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    // 保留系统中的ContentType, 添加新的Type在外部适应不同情况
    NSMutableSet *set = [NSMutableSet setWithObject:manager.responseSerializer.acceptableContentTypes];
    [set addObject:[NSSet setWithObject:@"text/html"]];
    [manager.responseSerializer setAcceptableContentTypes:set];
    //
    [manager GET:kAllCarDataURL parameters:@{@"name": @"好利来"} progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.URL);
        NSLog(@"请求成功:%@", [responseObject class]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", task.currentRequest.URL);
        NSLog(@"请求失败:%@", error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
